﻿<template>
  <a-modal
    title="新增数据库服务"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入标题"
            v-decorator="['title', { rules: [{ required: true, message: '请输入标题！' }] }]"
          />
        </a-form-item>
        <a-form-item label="数据库类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            style="width: 100%"
            placeholder="请选择数据库类型"
            v-decorator="['dbType', { rules: [{ required: true, message: '请选择数据库类型！' }] }]"
          >
            <a-select-option v-for="(item, index) in dbTypeData" :key="index" :value="item.code">{{
              item.name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="数据库版本" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入数据库版本"
            v-decorator="['dbServerVersion', { rules: [{ required: true, message: '请输入数据库版本！' }] }]"
          />
        </a-form-item>
        <a-form-item label="服务器IP或主机名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入服务器IP或主机名"
            v-decorator="['host', { rules: [{ required: true, message: '请输入服务器IP或主机名！' }] }]"
          />
        </a-form-item>
        <a-form-item label="数据库连接" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input
            placeholder="请输入数据库连接"
            v-decorator="['options', { rules: [{ required: true, message: '请输入数据库连接！' }] }]"
          />
        </a-form-item>
        <a-form-item label="数据库服务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库服务名称" v-decorator="['serverName', { rules: [{ required: true, message: '请输入数据库服务名称' }] }]" />
        </a-form-item>
        <a-form-item label="Agent身份ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入Agent身份ID" v-decorator="['agentId', { rules: [{ required: true, message: '请输入Agent身份ID' }] }]" />
        </a-form-item>
        <a-form-item label="通讯端口号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入通讯端口号" style="width: 100%" v-decorator="['agentPort']" />
        </a-form-item>
        <a-form-item label="Agent密钥" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入Agent服务身份验证密钥" v-decorator="['agentSecretKey', { rules: [{ required: true, message: '请输入Agent服务身份验证密钥' }] }]" />
        </a-form-item>
        <a-form-item label="alive消息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入alive消息" v-decorator="['aliveMessage']" />
        </a-form-item>
        <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isEnabled', { valuePropName: 'checked' }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { DbServerAdd } from '@/api/modular/main/DbServerManage'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 },
      },
      dbTypeData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
    }
  },
  methods: {
    // 初始化方法
    add(record) {
      this.visible = true
      const dbTypeOption = this.$options
      this.dbTypeData = dbTypeOption.filters['enumData']('DbType')
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            // WARN: 有可能导致类型错误，注意
            if (values[key] !== null && typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          DbServerAdd(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败：' + JSON.stringify(res.message))
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
